import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: '/apiTools/',
  routes: [{
    path: '/',
    redirect: '/login'
  },
    {
      path: '/login',
      component: () =>
        import('@/views/system/user/login')
    },
    {
      path: '/register',
      component: () =>
        import('@/views/system/user/register')
    },
    {
      path: '/home',
      component: () =>
        import('@/views/common/home'),
      redirect: '/userInfo',
      children: [{
        path: '/userInfo',
        component: () =>
          import ('@/views/system/user/userInfo')
      },]
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') {
    return next()
  }
  const token = sessionStorage.getItem('token')
  if (!token) {
    sessionStorage.setItem('msg', '请重新登录！')
    return next('/login')
  }
  next()
})

export default router
